from typing import Optional
from fastapi import APIRouter, HTTPException, status, Depends
from pydantic import BaseModel
from database import get_db

router = APIRouter(
    prefix="/statistics",
    tags=["statistics"],
)

@router.get("/admin/customer_count")
def get_customer_count(conn=Depends(get_db)):
    cursor = conn.cursor(dictionary=True)
    try:
        cursor.callproc("sp_GetCustomerCount")
        for result in cursor.stored_results():
            customer_count = result.fetchone()
        return customer_count
    finally:
        cursor.close()

@router.get("/admin/card_count")
def get_card_count(conn=Depends(get_db)):
    cursor = conn.cursor(dictionary=True)
    try:
        cursor.callproc("sp_GetCardCount")
        for result in cursor.stored_results():
            card_count = result.fetchone()
        return card_count
    finally:
        cursor.close()

@router.get("/admin/trade_count")
def get_trade_count(conn=Depends(get_db)):
    cursor = conn.cursor(dictionary=True)
    try:
        cursor.callproc("sp_GetTradeCount")
        for result in cursor.stored_results():
            trade_count = result.fetchone()
        return trade_count
    finally:
        cursor.close()

@router.get("/admin/trade_sum")
def get_trade_sum(conn=Depends(get_db)):
    cursor = conn.cursor(dictionary=True)
    try:
        cursor.callproc("sp_GetTradeSum")
        for result in cursor.stored_results():
            trade_sum = result.fetchone()
        return trade_sum
    finally:
        cursor.close()

@router.get("/admin/trade_statistics")
def get_trade_statistics(conn=Depends(get_db)):
    cursor = conn.cursor(dictionary=True)
    try:
        cursor.callproc("sp_GetTradeStatistics")
        for result in cursor.stored_results():
            trade_statistics = result.fetchone()
        return trade_statistics
    finally:
        cursor.close()

@router.get("/by-customer/{customer_id}/card-count")
def get_card_count_by_customer_id(customer_id: int, conn=Depends(get_db)):
    cursor = conn.cursor(dictionary=True)
    try:
        cursor.callproc("sp_GetCardCountByCustomerID", (customer_id,))
        for result in cursor.stored_results():
            card_count = result.fetchone()
        return card_count
    finally:
        cursor.close()

@router.get("/by-customer/{customer_id}/total-asset")
def get_total_asset_by_customer_id(customer_id: int, conn=Depends(get_db)):
    cursor = conn.cursor(dictionary=True)
    try:
        cursor.callproc("sp_GetTotalAssetByCustomerID", (customer_id,))
        for result in cursor.stored_results():
            total_asset = result.fetchone()
        return total_asset
    finally:
        cursor.close()

@router.get("/by-customer/{customer_id}/monthly-income")
def get_monthly_income_by_customer_id(customer_id: int, conn=Depends(get_db)):
    cursor = conn.cursor(dictionary=True)
    try:
        cursor.callproc("sp_GetMonthlyIncomeByCustomerID", (customer_id,))
        for result in cursor.stored_results():
            monthly_income = result.fetchone()
        return monthly_income
    finally:
        cursor.close()

@router.get("/by-customer/{customer_id}/monthly-expense")
def get_monthly_expense_by_customer_id(customer_id: int, conn=Depends(get_db)):
    cursor = conn.cursor(dictionary=True)
    try:
        cursor.callproc("sp_GetMonthlyExpenseByCustomerID", (customer_id,))
        for result in cursor.stored_results():
            monthly_expense = result.fetchone()
        return monthly_expense
    finally:
        cursor.close()
